
library(data.table)
library(lfe)
library(stargazer)
library(starpolishr)
library(ggplot2)
library(ggpubr)
library(MASS)

rm(list=ls())
setwd("C:/Users/k2258581/OneDrive - King's College London/Drive/Research/Partisan Motivated Reasoning/Replication package")


#import data
fData <- fread("Raw data ANES.csv")

#select variables to be included
vVars <- c("VCF0004","VCF0006a","VCF0009z","VCF0010z","VCF0011z","VCF0019",
           "VCF0050a","VCF0050b","VCF0101","VCF0104","VCF0105a","VCF0109",
           "VCF0110","VCF0114","VCF0115","VCF0116","VCF0202",
           "VCF0201","VCF0301","VCF0302","VCF0211","VCF0212",
           "VCF0803","VCF0650","VCF0604","VCF0656","VCF0503","VCF0504","VCF0731","VCF0733","VCF0310",
           "VCF0313"
           )

#give variable names
vLabels <- c("Year","Unique_ID","Weights_1","Weights_2","Weights_3","Relationship_head",
             "Political_knowledge_pre","Political_knowledge_post",
             "Age","Gender","Race","Ethnicity","Education","Family_income_group",
             "Occupation_group","Employment_status","Republican_thermometer",
             "Democrat_thermometer","Party_id","Party_id_initial",
             "Liberal_thermometer","Conservative_thermometer","Ideology",
             "Performance_fedgov","Trust_fedgov","Trust_gov_index",
             "Dem_libcons_perception","Rep_libcons_perception","Discuss_politics_fam","How_often_discuss_politics_fam",
             "Interest_elections","Interest_public_affairs")

fBoth <- data.frame(Variable = vVars, Label = vLabels)

fData <- fData[,..vVars]
colnames(fData) <- vLabels

fData <- fData[Year>=1974] #same years as GSS analysis



#party identification
fData <- fData[,Party_cat := ifelse(Party_id%in%1:3,"Democrat",
                                    ifelse(Party_id%in%5:7,"Republican",
                                           ifelse(Party_id==4,"Independent",NA)))]

fData <- fData[,Dem := Party_cat=="Democrat"]
fData <- fData[,Rep := Party_cat=="Republican"]
fData <- fData[,Ind := Party_cat=="Independent"]



fData <- fData[,Ideology := ifelse(Ideology%in%1:7,Ideology,NA)]
fData <- fData[,Dem_libcons_perception := ifelse(Dem_libcons_perception%in%1:7,Dem_libcons_perception,NA)]
fData <- fData[,Rep_libcons_perception := ifelse(Rep_libcons_perception%in%1:7,Rep_libcons_perception,NA)]

fData <- fData[,Perceived_distance_rep := Ideology-Rep_libcons_perception]
fData <- fData[,Perceived_distance_rep_abs := abs(Ideology-Rep_libcons_perception)]
fData <- fData[,Perceived_distance_dem := Ideology-Dem_libcons_perception]
fData <- fData[,Perceived_distance_dem_abs := abs(Ideology-Dem_libcons_perception)]

#cleaning
fData <- fData[Party_cat=="",Party_cat:=NA]



fwrite(fData,"ANES_clean.csv")






